# -*- coding:utf-8  -*-

import requests
from lxml import etree
from fake_useragent import UserAgent
from http import cookiejar
import json
from datetime import datetime
from utils.HttpUtils import HttpUtils

requests.packages.urllib3.disable_warnings()  # 忽略HTTPS安全警告

'''
京东商城
1.url为搜索商品的请求地址
2.sku_url为提取商品的SKU拼接再次请求，返回评价信息
'''
class JD():
    def __init__(self):
        #声明一个CookieJar对象实例来保存cookie
        self.cookie = cookiejar.CookieJar()
        ua = UserAgent(use_cache_server=False)  # 禁用服务器缓存
        self.headers = {
            # "User-Agent": ua.random,
            'authority': 'search.jd.com',
            'method': 'GET',
            'path': '/s_new.php?keyword=%E6%89%8B%E6%9C%BA&enc=utf-8&qrst=1&rt=1&stop=1&vt=2&page=3&s=61&click=0',
            'scheme': 'https',
            'accept': '*/*',
            'accept-encoding': 'gzip, deflate, br',
            'accept-language': 'zh-CN,zh;q=0.9',
            'cookie': '__jdv=76161171|direct|-|none|-|1587524031773; __jdu=15875240317721174661881; areaId=8; ipLoc-djd=8-579-37581-0; PCSYCityID=CN_210000_210300_210303; shshshfpa=950047b2-fc44-f89d-9c85-a3ac37d13b39-1587524033; shshshfpb=zEw5OsesudZRf2Sjec897mA%3D%3D; xtest=869.cf6b6759; __jda=122270672.15875240317721174661881.1587524032.1587524032.1587524032.1; __jdb=122270672.2.15875240317721174661881|1.1587524032; __jdc=122270672; shshshfp=ee744e5ef0c9f23866f02e95c2695096; shshshsID=921ec76de5ba32ae5d135d13cb07dfce_2_1587524043060; qrsc=1; rkv=V0500; 3AB9D23F7A4B3C9B=FI6XPVQSX4BB3JFUAHW25AO2Y4I3O44RJSKJ5SVBA5PY4J2UCH6G7QT6TYICDHLRTIS7EA3HT24Q44A3DQOFX5GPMI',
            'dnt': '1',
            'referer': 'https://search.jd.com/Search?keyword=%E6%89%8B%E6%9C%BA&enc=utf-8&qrst=1&rt=1&stop=1&vt=2&page=3&s=61&click=0',
            'sec-fetch-dest': 'empty',
            'sec-fetch-mode': 'cors',
            'sec-fetch-site': 'same-origin',
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36',
            'x-requested-with': 'XMLHttpRequest'
        }

    def get_contents(self):
        # 搜索商品URL
        url = "https://search.jd.com/s_new.php?keyword=%E6%89%8B%E6%9C%BA&enc=utf-8&qrst=1&rt=1&stop=1&vt=2&page=5&s=61&click=0"
        # url = "https://search.jd.com/search?keyword=U%E7%9B%98&enc=utf-8&qrst=1&rt=1&stop=1&vt=2&wq=U%E7%9B%98&ev=exprice_50-100%5E&page=3&s=55&click=0"
        url = "https://search.jd.com/s_new.php?keyword=%E9%80%9F%E5%B9%B2%E8%BF%90%E5%8A%A8%E8%A1%A3&enc=utf-8&qrst=1&rt=1&stop=1&vt=2&wq=%E9%80%9F%E5%B9%B2%E8%BF%90%E5%8A%A8%E8%A1%A3&page=3&s=56&click=0"
        html = HttpUtils.do_request("GET", url, self.headers, "")
        html.encoding = 'utf-8'
        selector = etree.HTML(html.text)
        data_list = []
        sku_all = ""
        for c in selector.xpath('//li[@class="gl-item"]'):
            dict_data = dict()
            dict_data['sku'] = c.xpath('./@data-sku')[0]
            sku_all += dict_data["sku"] + ","
            dict_data['price'] = c.xpath('./div/div[@class="p-price"]/strong/i/text()')[0]
            dict_data['title'] = ''.join(c.xpath('./div/div[@class="p-name p-name-type-2"]/a/em/text()'))
            dict_data['url'] = c.xpath('./div/div[@class="p-name p-name-type-2"]/a/@href')[0]
            dict_data['shop'] = "".join(c.xpath('./div/div[@class="p-shop"]/span//text()'))
            data_list.append(dict_data)
        # 评价情况链接(根据商品的SKU拼接后请求，返回SKU相关的评价情况)
        sku_url = "https://club.jd.com/comment/productCommentSummaries.action?referenceIds=" + sku_all[:-1] + "&callback=jQuery3638156&_=1572871730411"
        sku_html = HttpUtils.do_request("GET", sku_url, self.headers, "")
        sku_html = sku_html.text.replace("jQuery3638156", "").replace("(", "").replace(")", "").replace(";", "")
        data_json = json.loads(sku_html)
        for d in data_json['CommentsCount']:
            CommentCount = d['CommentCount']  # 总评价数量
            AverageScore = d['AverageScore']  # 平均得分
            GoodCount = d['GoodCount']  # 好评数
            DefaultGoodCount = d['DefaultGoodCount']  # 默认好评
            GoodRate = d['GoodRate']  # 好评率
            AfterCount = d['AfterCount']  # 追评数
            VideoCount = d['VideoCount']  # 视频晒单数
            PoorCount = d['PoorCount']  # 差评数
            GeneralCount = d['GeneralCount']  # 中评数


    # 获取评价内容、评价标签
    def get_comments_tag(self, productId):
        ua = UserAgent(use_cache_server=False)  # 禁用服务器缓存
        headers = {
            "User-Agent": ua.random,
        }
        postData = {
            'callback': 'fetchJSON_comment98',
            'productId': productId,
            'score': '0',
            'sortType': '5',
            'page': '0',
            'pageSize': '10',
            'isShadowSku': '0',
            'fold': '1'
        }
        url = "https://club.jd.com/comment/productPageComments.action"
        html = HttpUtils.do_request("GET", url, headers, postData)
        data_html = html.text.replace("fetchJSON_comment98", "").replace("(", "").replace(")", "").replace(";", "")
        data_json = json.loads(data_html)
        dict_data = dict()
        # 评价标签
        tag_num = 0
        for d in data_json['hotCommentTagStatistics']:
            tag_num += 1
            dict_data['tag_name_' + str(tag_num)] = d['name']
            dict_data['tag_count_' + str(tag_num)] = d['count']
        # 各个评价数量
        dict_data['commentCount'] = data_json['productCommentSummary']['commentCount']  # 总评价数量
        dict_data['videoCount'] = data_json['productCommentSummary']['videoCount']  # 视频评价数量
        dict_data['afterCount'] = data_json['productCommentSummary']['afterCount']  # 追评数量
        dict_data['goodCount'] = data_json['productCommentSummary']['goodCount']  # 好评数量
        dict_data['generalCount'] = data_json['productCommentSummary']['generalCount']  # 中评数量
        dict_data['poorCount'] = data_json['productCommentSummary']['poorCount']  # 差评数量
        dict_data['goodRate'] = data_json['productCommentSummary']['goodRate']  # 好评百分比
        dict_data['generalRate'] = data_json['productCommentSummary']['generalRate']  # 中评百分比
        dict_data['poorRate'] = data_json['productCommentSummary']['poorRate']  # 差评百分比
        # 用户评价
        for c in data_json['comments']:
            dict_data = dict()
            dict_data['content'] = c['content']  # 评论内容
            dict_data['creationTime'] = c['creationTime']  # 评论时间
            dict_data['nickname'] = c['nickname']  # 用户昵称
            dict_data['productColor'] = c['productColor']  # 购买商品具体哪款
            dict_data['referenceName'] = c['referenceName']  # 购买商品
            dict_data['productSize'] = c['productSize']  # 购买数量
            dict_data['anonymousFlag'] = c['anonymousFlag']  # PLUS会员：1：是，0：否
            dict_data['score'] = c['score']  # 用户评分


if __name__ == '__main__':
    jd = JD()
    # jd.get_contents()
    jd.get_comments_tag("29500709665")